﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using HZ.Supervise.Model;

namespace HZ.Supervise.Web.Secondphase.ReportCompany
{
    public partial class CurrentGoodsListSC : BasePage
    {
        private string strMosth = "";//月份
        private string strcompanysc = "";//生产企业
        string order = "PuchaseCountSum asc";
        string append = "";
        protected void Page_Load(object sender, EventArgs e)
        {


            if (!Page.IsPostBack)
            {
                ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
                BindList();
            }


        }
        void BindList()
        {

            SuperviseQuery query = ViewState["query"] as SuperviseQuery;

            strcompanysc = query.CompanyName_SC;
            strMosth = query.Mid;

            StringBuilder strb = new StringBuilder();
            string strsortname = SortControls1.SortName;
            if (strsortname != "")
            {
                strb.Append(" and sortname like '%" + strsortname + "%'");
            }
            if (!strMosth.Equals(""))
            {
                strb.Append(" and " + Function.FormatPlanTime("addtime", strMosth) + " and Sellname='" + strcompanysc + "'");
            }
            int t = 0;
            HZ.Supervise.IBLL.IBll_CompanyOrderDetail ibcd = HZ.Supervise.BLLFactory.IBll_CompanyOrderDetail();
            strb.Append(append);
            DataTable dt = ibcd.GetCompanySC_sort(0, strb.ToString(),order, 1, 0, out t);

            //显示相应的字段
            gvwSortInfo.DataSource = dt;

            gvwSortInfo.DataBind();
            for (int j = 0; j < gvwSortInfo.Rows.Count; j++)
            {
                //名称相同才能合并
                //防止超出索引
                if (j < (gvwSortInfo.Rows.Count - 1))
                {
                    if (gvwSortInfo.Rows[j].Cells[0].Text.Trim() == gvwSortInfo.Rows[j + 1].Cells[0].Text.Trim())
                    {
                        //合并头
                        gvwSortInfo.Rows[j].Cells[0].RowSpan = 2;
                        gvwSortInfo.Rows[j + 1].Cells[0].Visible = false;

                        //合并尾
                        gvwSortInfo.Rows[j].Cells[7].RowSpan = 2;
                        gvwSortInfo.Rows[j + 1].Cells[7].Visible = false;
                        j++;
                    }
                    else
                    {

                    }
                }

                //合并

            }
        }
        protected void gvwSortInfo_RowCreated(object sender, GridViewRowEventArgs e)
        {
            #region 合并表头
            switch (e.Row.RowType)
            {
                case DataControlRowType.Header:

                    //取得下拉框的月份


                    //第一行表头
                    TableCellCollection tcHeader = e.Row.Cells;
                    tcHeader.Clear();
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[0].Attributes.Add("colspan", "2"); //跨Row
                    tcHeader[0].Attributes.Add("bgcolor", "LightSteelBlue");
                    //tcHeader[0].Attributes.Add("bgcolor", "white");
                    tcHeader[0].Text = "";
                    //tcHeader.Add(new TableHeaderCell());
                    //tcHeader[1].Attributes.Add("bgcolor", "Red");
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[1].Attributes.Add("colspan", "4"); //跨Column
                    tcHeader[1].Text = strMosth;

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[2].Attributes.Add("colspan", "2"); //跨Column
                    tcHeader[2].Text = "</th></tr><tr>";



                    //第2行表头
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[3].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[3].Text = "类别";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[4].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[4].Text = "是否基药";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[5].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[5].Text = "生产企业供货总数量";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[6].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[6].Text = "生产企业供货总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[7].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[7].Text = "操作</th></tr><tr>";
                    break;
            }
            #endregion
        }

        protected void gvwSortInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
               
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindList();
        }

        protected void gvwSortInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "CompanyGoodsList_SC")
            {
                string sessionid = Guid.NewGuid().ToString();
                SuperviseQuery query = ViewState["query"] as SuperviseQuery;

                query.SortName = e.CommandArgument.ToString();

                Session[sessionid] = query;

                if (!string.IsNullOrEmpty(query.SortName))
                {
                    Response.Redirect(string.Format("CompanyGoodsList_sc.aspx?sessionid={0}", sessionid));
                }
                else
                {

                }
            }
        }

        protected void BtnOK_Click(object sender, EventArgs e)
        {

            if (!this.droSort.SelectedValue.Equals("-1"))
            {
                append = "AND Source = " + droSort.SelectedValue;
            }
            else
            {
                append = "";
            }
            order = this.droSortField.SelectedValue;

            BindList();
        }
    }
}
